package com.ruoyi.common.utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;

import org.apache.commons.io.IOUtils;
/**
 * @创建者：zhouwei
 * @创建时间：2022/7/25
 * @描述：
 */
public class CommandExecute {
	public static String executeCommand(String command) {
		StringBuffer output = new StringBuffer();
		Process p;
		InputStreamReader inputStreamReader = null;
		BufferedReader reader = null;
		try {
			p = Runtime.getRuntime().exec(command);
			p.waitFor();
			inputStreamReader = new InputStreamReader(p.getInputStream(), StandardCharsets.UTF_8);
			reader = new BufferedReader(inputStreamReader);
			String line = "";
			while ((line = reader.readLine()) != null) {
				output.append(line + "\n");
			}
			//p.destroy();//这个一般不需要
		} catch (IOException e) {
			e.printStackTrace();
		} catch (InterruptedException e) {
			e.printStackTrace();
		} finally {
			IOUtils.closeQuietly(reader);
			IOUtils.closeQuietly(inputStreamReader);
		}
		System.out.println(output.toString());
		return output.toString();

	}
}
